package component;

import javax.media.j3d.Appearance;

import toolbox.Toolbox;

import node.appearance.AppearanceFactory;
import node.appearance.MetalAppearanceFactory;

public class Status {
	public static final int NORMAL = 0;
	public static final int PLACEMENT = 1;
	public static final int HIGHLIGHTED = 2;
	public static final int ILLEGAL = 4;
	public static final int SELECTED = 8;
	public static final int TRANSPARENT = 16;
	public static final int INVISIBLE = 32;
	
	private static final String NORMAL_MATERIAL = MetalAppearanceFactory.METALLIC_PLAIN;
	private static final String ROOT_MATERIAL = MetalAppearanceFactory.GOLD;
	private static final String PLACEMENT_MATERIAL = MetalAppearanceFactory.METALLIC_GREEN;
	private static final String ILLEGAL_MATERIAL = MetalAppearanceFactory.METALLIC_LIGHT_RED;
	private static final String HIGHLIGHTED_MATERIAL = MetalAppearanceFactory.COPPER;
	private static final String SELECTED_MATERIAL = MetalAppearanceFactory.METALLIC_BLUE;
	private static final String SELECTED_HIGHLIGHTED_MATERIAL = MetalAppearanceFactory.METALLIC_PURPLE;

	private int statusFlag = 0;
	
	public Status() {
	}
	
	public Status(int flags) {
		statusFlag = flags;
	}
	
	public void copy(Status status) {
		this.statusFlag = status.getStatus();
	}
	
	public void set(int flag, boolean status) {
		this.statusFlag = (status ? statusFlag | flag : statusFlag & (~flag) );
	}
	
	public boolean is(int flag) {
		if (flag == NORMAL)
			return statusFlag == flag;
		return (statusFlag & flag) > 0;
	}
	
	public void setStatus(int flags) {
		this.statusFlag = flags;
	}
	
	public int getStatus() {
		return statusFlag;
	}
	
	public Appearance getAppearance(AbstractComponent component) {
		AppearanceFactory factory = Toolbox.appearanceFactory();
		String state = (statusFlag & TRANSPARENT) > 0 ? AppearanceFactory.TRANSPARENT_STATE :
			(statusFlag & INVISIBLE) > 0 ? AppearanceFactory.INVISIBLE_STATE : AppearanceFactory.SOLID_STATE;
		if ((statusFlag & SELECTED) > 0 && (statusFlag & HIGHLIGHTED) > 0) {
			return factory.getAppearance(state, SELECTED_HIGHLIGHTED_MATERIAL);
		} else if ((statusFlag & PLACEMENT) > 0) {
			return factory.getAppearance(state, PLACEMENT_MATERIAL);
		} else if ((statusFlag & ILLEGAL) > 0) {
			return factory.getAppearance(state, ILLEGAL_MATERIAL);
		} else if ((statusFlag & HIGHLIGHTED) > 0) {
			return factory.getAppearance(state, HIGHLIGHTED_MATERIAL);
		} else if ((statusFlag & SELECTED) > 0) {
			return factory.getAppearance(state, SELECTED_MATERIAL);
		} else {
			if (component.isRoot())
				return factory.getAppearance(state, ROOT_MATERIAL);
			else
				return factory.getAppearance(state, NORMAL_MATERIAL);
		}		
	}
}
